//
//  LKTimeDatePickerView.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/7/16.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKTimeDatePickerView.h"
#import <Masonry.h>
#import "LKDefineFile.h"
#import "LKBaseView.h"
@interface LKTimeDatePickerView()

@property (strong, nonatomic) UIDatePicker *datePicker;
@property (strong, nonatomic) UIDatePicker *rightDatePicker;
@property (strong, nonatomic) UIView *bottomView;
@property(nonatomic,copy)NSString *formatter;
@property (nonatomic,copy) void(^dateBlock)(NSDate *date,NSDate *rightdate);
@property (nonatomic,copy) void(^cancelBlock)();

@end

@implementation LKTimeDatePickerView
+ (void) showCustomDatePickerAtView:(UIView *)superView WithFomate:(NSString *)formatter
choosedDateBlock:(void (^)(NSDate *date,NSDate *rightdate))date
                        cancelBlock:(void(^)())cancel{
    LKTimeDatePickerView *picker = [[LKTimeDatePickerView alloc] init];
    picker.formatter = formatter;
    [superView addSubview:picker];
    picker.translatesAutoresizingMaskIntoConstraints = NO;
    [picker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(superView).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    if (date) {
        picker.dateBlock = date;
    }
    if (cancel) {
       picker.cancelBlock = cancel;
    }
}


-(instancetype)init{
    if (self = [super init]) {
      self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        [self setupUI];
    }
    return self;
}
//设置视图的界面
-(void)setupUI{
    self.datePicker = [[UIDatePicker alloc] init];
    _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    _datePicker.datePickerMode = UIDatePickerModeTime;
    self.rightDatePicker = [[UIDatePicker alloc] init];
    _rightDatePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    _rightDatePicker.datePickerMode = UIDatePickerModeTime;
    //定义最小日期
    NSDateFormatter *formatter_minDate = [[NSDateFormatter alloc] init];
//    [formatter_minDate setDateFormat:@"yyyy年-MM月-dd日"];
    [formatter_minDate setDateFormat:@"MM-dd HH:mm"];
    NSTimeZone* sourceTimeZone = [NSTimeZone systemTimeZone];
    formatter_minDate.timeZone = sourceTimeZone;
    NSDate *minDate = [formatter_minDate dateFromString:@"2004年-01月"];
    _datePicker.minimumDate = minDate;
    [self addSubview:_datePicker];
    [_datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self);
        make.width.mas_equalTo(WIDTH/2 - 10);
        make.height.mas_equalTo(180);
    }];
    [self addSubview:_rightDatePicker];
    [_rightDatePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self);
        make.width.mas_equalTo(WIDTH/2 - 10);
        make.height.mas_equalTo(180);
    }];
    LKBaseLabel *bottomLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"~" textColor:[UIColor blackColor] font:[UIFont fontWithName:@"PingFangSC-Medium" size:17] textAlignment:NSTextAlignmentCenter];
    [self addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_datePicker.mas_right);
        make.right.equalTo(_rightDatePicker.mas_left);
        make.centerY.equalTo(_datePicker.mas_centerY);
        make.height.mas_equalTo(180);
    }];
    self.bottomView = [[UIView alloc] init];
    [self addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.bottom.equalTo(_datePicker.mas_top);
        make.height.mas_equalTo(40);
    }];
    UIColor *color = [UIColor colorWithRed:242/255.0 green:243/255.0 blue:249/255.0 alpha:1];
    UIColor *btnColor = [UIColor colorWithRed:65.0/255 green:164.0/255 blue:249.0/255 alpha:1];
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:btnColor forState:0];
    [_bottomView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(_bottomView);
        make.width.mas_equalTo(60);
    }];
    UIButton *surelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [surelBtn setTitle:@"确定" forState:UIControlStateNormal];
    [surelBtn setTitleColor:btnColor forState:0];
    [_bottomView addSubview:surelBtn];
    [surelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(_bottomView);
        make.width.mas_equalTo(60);
    }];
    cancelBtn.tag = 1010;
    surelBtn.tag = 1020;
    [surelBtn addTarget:self action:@selector(ChooseOrNot:) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn addTarget:self action:@selector(ChooseOrNot:) forControlEvents:UIControlEventTouchUpInside];
    _bottomView.backgroundColor = color;
    _datePicker.backgroundColor = color;
    _rightDatePicker.backgroundColor = color;
    bottomLabel.backgroundColor = color;
//    self.backgroundColor = color;
}
//按钮选择
-(void)ChooseOrNot:(UIButton *)btn{
    if (btn.tag == 1010) {
        if (self.cancelBlock) {
            self.cancelBlock();
        }
        [self removeFromSuperview];
    }
    if (btn.tag == 1020) {
        if (self.dateBlock) {
            self.dateBlock(self.datePicker.date,self.rightDatePicker.date);
        }
        [self removeFromSuperview];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
